package com.lss233.recipeguide;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @author Lss233
 */
public class Storage {
    private static FileConfiguration file;
    private static File confile = new File(RecipeGuide.getInstance().getDataFolder(),"recipes.yml");
    public static void reload() {

        if(!confile.exists())
            try {
                confile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        file = YamlConfiguration.loadConfiguration(confile);
    }
    public static void saveRecipe(RecipeData data) throws IOException {
        String key = data.getResultItem().getType().toString()+"-"+System.currentTimeMillis();
        file.set(key+".recipeItems",data.getRecipeItems());
        file.set(key+".resultItems",data.getResultItem());
        file.save(confile);

    }

    public static Set<String> getRecipeList() {
        return file.getKeys(false);
    }
    public static RecipeData getRecipe(String key){
        RecipeData rd = new RecipeData();
        rd.setRecipeItems((List<ItemStack>) file.get(key+".recipeItems"));
        rd.setResultItem((ItemStack) file.get(key+".resultItems"));
        return rd;
    }
}
